#pragma once
#include <iostream>
#include <string>
#include <jsoncpp/json/json.h>
const std::string black_space_sep = " ";
const std::string protocol_sep = "\n";
std::string Encode(std::string &content) // 加报头
{
    ///"len"\n"内容"\n
    std::string package = std::to_string(content.size());
    package += protocol_sep;
    package += content;
    package += protocol_sep;
    return package;
}
bool Decode(std::string &package, std::string *content) // 去掉报头
{
    size_t pos = package.find(protocol_sep);
    if (pos == std::string::npos)
        return false;
    std::string len_str = package.substr(0, pos);
    if (len_str == "")
        return false;
    size_t len = stoi(len_str);
    size_t total_len = len + 2 + len_str.size();
    if (package.size() < total_len)
        return false;
    *content = package.substr(pos + 1, len);
    package.erase(0, total_len);
    return true;
}

class Request // 请求
{
public:
    Request(int data1, int data2, char oper)
        : x(data1), y(data2), op(oper)
    {
    }
    Request()
    {}

public:
    // 序列化
    //"x op y"
    bool Serialize(std::string *out)
    {
#ifdef myself
        std::string s = std::to_string(x);
        s += black_space_sep;
        s += op;
        s += black_space_sep;
        s += std::to_string(y);
        *out = s;
        return true;
#else
        Json::Value root;
        root["x"] = x;
        root["y"] = y;
        root["op"] = op;
        //Json::FastWriter w;
        Json::StyledWriter w;
        *out = w.write(root);
        std::cout<<*out<<std::endl;
        return  true;

#endif
    }
    bool Deserialize(const std::string &in) // 去除序列化
    {
#ifdef myself
        int left = in.find(black_space_sep);
        if (left == std::string::npos)
            return false;
        int right = in.rfind(black_space_sep);
        if (right == std::string::npos)
            return false;
        if (left + 2 != right)
            return false;
        x = stoi(in.substr(0, left));
        op = in[left + 1];
        y = stoi(in.substr(right + 1));
        return true;
#else
        Json::Value v;
        Json::Reader r;
        r.parse(in,v);
        x=v["x"].asInt();
        y=v["y"].asInt();
        op=v["op"].asInt();
        return true;
#endif
    }
    void Print_req()
    {
        std::cout << x << op << y << "=?" << std::endl;
    }

public:
    int x = 0;
    int y = 0;
    char op;
    /* data */
};

class Response
{
public:
    Response(int re = 0, int co = 0)
        : result(re), code(co)
    {
    }
    //"result code"
    bool Serialize(std::string *out)
    {
       
#ifdef myself
        std::string s = std::to_string(result);
        s += black_space_sep;
        s += std::to_string(code);
        *out = s;
        return true;
#else
        Json::Value root;
        root["result"] = result;
        root["code"] = code;
        //Json::FastWriter w;
        Json::StyledWriter w;
        *out = w.write(root);
        std::cout<<*out<<std::endl;
        return  true;

#endif
    }
    bool Deserialize(const std::string &in) // 去除序列化
    {
       
#ifdef myself
        int pos = in.find(black_space_sep);
        if (pos == std::string::npos)
            return false;
        result = stoi(in.substr(0, pos));
        code = stoi(in.substr(pos + 1));
        return true;
#else
        Json::Value root;
        Json::Reader a;
        a.parse(in,root);
        result=root["result"].asInt();
        result=root["code"].asInt();
        return  true;

#endif
    }
    void Print_result()
    {
        std::cout << "result:" << result << "code" << code << std::endl;
    }

public:
    int result;
    int code; // 计算结果是否正常
};
